package com.yunshipei.utils;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;

import com.fsck.k9.activity.Accounts;
import com.yunshipei.common.Globals;
import com.yunshipei.manager.YspPreferences;
import com.yunshipei.ui.activity.SignInActivity;
import com.yunshipei.ui.activity.WarnActivity;

public class YspProtocolUtils {

    public static void openEmailClient(Context context, String url) {
        //先判断Enterplorer邮箱是不是配置了，如果没配置则调用本地邮箱
        boolean emailAvailable = YspPreferences.getInstance().getSharedPreferences().getBoolean(Globals.YSP_EMAIL_AVAILABLE, false);
        if (emailAvailable) {
            openNativeEmail(context);
        } else {
            Intent data = new Intent(Intent.ACTION_SENDTO);
            data.setData(Uri.parse(url));
            data.putExtra(Intent.EXTRA_SUBJECT, "");
            data.putExtra(Intent.EXTRA_TEXT, "");
            context.startActivity(data);
        }
    }

    public static void openNativeEmail(Context mContext) {
        mContext.startActivity(new Intent(mContext, Accounts.class));
    }

    public static void ringUp(Context mContext, String phoneStr) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_DIAL);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setData(Uri.parse(phoneStr));
        mContext.startActivity(intent);
    }

    public static void launchNativeApp(Context mContext, String packageName) {
        Intent intent = mContext.getPackageManager().getLaunchIntentForPackage(packageName);
        if (intent == null) {
            // Bring user to the market or let them choose an app?
            intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse("market://details?id=" + packageName));
        }
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mContext.startActivity(intent);
    }

    public static void launchWarnFunc(Context mContext) {
        Intent intent = new Intent();
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
        intent.setClass(mContext, WarnActivity.class);
        mContext.startActivity(intent);
    }

    public static void launchSignIn(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, SignInActivity.class);
        context.startActivity(intent);

    }
}
